TAG_SEL_BACK = 6666;
TAG_ROOT_NODE = 6667;
var /**
	 * 选择页面
	 */
SelectLayer = cc.Layer.extend({
	arr:null,
	cell:null,
	curPos:0,
	maxPos:6,
	scene:null,
	labelArr:[],
	lineArr:[],
	// 当前任务的节点
	flowNodeNo: -1,
	ctor:function (parent) {
		this._super();
		parent.addChild(this, 200);
		this.init();
	},
	init:/**
			 * 初始化,
			 */
	function () {
		this._super();
		cc.log("SelectLayer");
		this.setCascadeOpacityEnabled(true);
		ll.tool.hiddleLib();
		ll.tip.hiddleTip();
		gg.nodeArrs = [];
		
		for(var i in teachFlow){
			var flow = teachFlow[i];
			if(flow.node){
				gg.nodeArrs.push(flow);
			}
		}
		this.initBack();
		this.initContent();
		this.initVer();
		this.show();
		this.loadSlide();
	},
	initBack:function(){
		var topLine = new cc.Sprite("#top_line.png");
		topLine.setPosition(gg.c_width, 642);
		this.addChild(topLine, 10);
		var back = new cc.Sprite("#sel_bg.png");
		back.setAnchorPoint(0.5, 1);
		back.setPosition(gg.c_width, 642);
		this.addChild(back,5, TAG_SEL_BACK);
		back.setScaleY(0);
		back.setCascadeOpacityEnabled(true);
	},
	initContent:function(){
		var back = this.getChildByTag(TAG_SEL_BACK);
		var rootNode = new cc.Node();
		rootNode.setAnchorPoint(0, 1);
		rootNode.setPosition(210, 380);
		rootNode.setCascadeOpacityEnabled(true);
		back.addChild(rootNode,50,TAG_ROOT_NODE);
		this.select = new Angel(rootNode,"#sp2.png");
		this.select.setVisible(false);
		var first = true;
		var prev = null;
		var marign = 15;
		for(var i in gg.nodeArrs){
			var node = gg.nodeArrs[i];
			var label = new Label(rootNode,node.nodeNo + "    " + node.nodeName, this.callback, this);
			label.setAnchorPoint(0, 0.5);
			label.setTag(node.id); // 步数
			label.nodeNo = node.nodeNo; // 章数
			if(first){
				first = false;
			} else {
				label.down(prev, marign);
			}
			this.labelArr[i] = label;
			
			var line = new Angel(rootNode,"#marign_line.png");
			line.setAnchorPoint(0, 0.5);
			line.down(label, marign);
			this.lineArr[i] = line;
			prev = line;
			this.cell = label.height + line.height + marign * 2;
			
			if(i >= this.maxPos){
				this.hiddleArr(i);
			}
		}
	},
	initVer:function(){
		var back = this.getChildByTag(TAG_SEL_BACK);
		// 空心条子
		this.ver = new cc.Sprite("#ver_line.png");
		this.ver.setPosition(640,220);
		back.addChild(this.ver, 60);
		this.ver.setCascadeOpacityEnabled(true);
		// 位置点
		this.pos = new cc.Sprite("#sp1.png");
		this.pos.setPosition(this.ver.width * 0.5, this.ver.height);
		this.ver.addChild(this.pos, 3);
		// 实心条子
		this.ver2 = new cc.Sprite("#ver_line2.png");
		this.ver2.setAnchorPoint(0.5,1);
		this.ver2.setPosition(this.ver.width * 0.5, this.ver.height);
		this.ver2.setScaleY(0);
		this.ver.addChild(this.ver2, 2);
	},
	gotoL:function(){
		this.flowNodeNo = gg.flow.flow.nodeNo;
		var count = this.flowNodeNo - this.curPos - 1;
		for(var i = 0; i < count; i++){
			this.up();
		}
		this.select.setVisible(true);
		this.select.left(this.labelArr[this.flowNodeNo - 1], 16);
		this.select.local = this.flowNodeNo - 1;
	},
	up:function (){
		// 序号减少
		if(this.curPos <= 0){
			return;
		}
		var cur = --this.curPos;
		var node = this.getChildByTag(TAG_SEL_BACK).getChildByTag(TAG_ROOT_NODE);
		var seq = cc.sequence(cc.moveBy(0.2,cc.p(0, -this.cell)), cc.callFunc(function(){
			this.refreshShow(true, cur);
		}, this));
		node.runAction(seq);

	},
	down:function (){
		// 序号增加
		if(this.curPos >= gg.nodeArrs.length - 1){
			return;
		}
		var cur = this.curPos++; 
		var node = this.getChildByTag(TAG_SEL_BACK).getChildByTag(TAG_ROOT_NODE);
		var seq = cc.sequence(cc.moveBy(0.2,cc.p(0, this.cell)), cc.callFunc(function(){
			this.refreshShow(false, cur);
		}, this));
		node.runAction(seq);
	},
	refreshShow:function(down, cur){
		cc.log("this.curPos : " + cur);
		if(down){
			this.fadeinArr(cur);
			if(cur + this.maxPos < this.lineArr.length){
				this.fadeoutArr(cur + this.maxPos);
			}
			var ym = this.ver.height / (this.maxPos - 1);
			this.pos.runAction(cc.moveBy(0.2, cc.p(0, ym)));
			this.ver2.runAction(cc.scaleTo(0.2, 1, this.curPos / (this.maxPos - 1)));
		} else {
			this.fadeoutArr(cur);
			if(cur + this.maxPos < this.lineArr.length){
				this.fadeinArr(cur + this.maxPos);
			}
			var ym = this.ver.height / (this.maxPos - 1);
			this.pos.runAction(cc.moveBy(0.2, cc.p(0, -ym)));
			this.ver2.runAction(cc.scaleTo(0.2, 1, this.curPos / (this.maxPos - 1)));
		}
	},
	hiddleArr:function(index){
		this.labelArr[index].setOpacity(0);
		this.lineArr[index].setOpacity(0);
	},
	showArr:function(index){
		this.labelArr[index].setOpacity(255);
		this.lineArr[index].setOpacity(255);
	},
	fadeinArr:function(index){
		this.labelArr[index].runAction(cc.fadeIn(0.4));
		this.lineArr[index].runAction(cc.fadeIn(0.4));
		if(this.select.local == index){
			this.select.runAction(cc.fadeIn(0.4));
		}
	},
	fadeoutArr:function(index){
		this.labelArr[index].runAction(cc.fadeOut(0.4));
		this.lineArr[index].runAction(cc.fadeOut(0.4));
		if(this.select.local == index){
			this.select.runAction(cc.fadeOut(0.4));
		}
	},
	show:function(){
		var back = this.getChildByTag(TAG_SEL_BACK);
		var seq = cc.sequence(cc.delayTime(0.5), cc.scaleTo(0.5, 1, 1));
		back.runAction(seq);
	},
	callback:function(p){
		this.select.setVisible(true);
		this.select.left(p, 16);
		/*- 节点未动,则从本章节第一步开始
		if(this.flowNodeNo == p.nodeNo){
			// 节点未动
			var seq = cc.sequence(cc.fadeOut(0.8),
					cc.callFunc(function(){
						ll.run.setVisible(true);
						ll.tip.arr.showArr(true);
						this.getParent().destroySelect();	
					}, this));
			this.runAction(seq);
		} else {
		}*/
		var seq = cc.sequence(cc.fadeOut(0.8),
				cc.callFunc(function(){
					ll.tip.arr.showArr();
					ll.tool.chapterSel();
					gg.flow.start(p.getTag() - 1);
					this.destroy();			
				}, this));
		this.runAction(seq);
	},
	destroy:/**
			 * 销毁
			 */
	function(){
		this.removeFromParent(true);
		ll.select = null;
	},
	onExit:function(){
		this._super();
		ll.tool.showLib();
		ll.tip.showTip();
	},
	loadSlide:/**
				 * 滑动监听
				 */
		function(){
		// 上下滑动监听
		var listener_touch = cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: false,
			hover: false,
			onTouchBegan:UpperLowerSliding.onTouchBegan,
			onTouchMoved:UpperLowerSliding.onTouchMoved,
			onTouchEnded:UpperLowerSliding.onTouchEnded});
		cc.eventManager.addListener(listener_touch, this);
		if ('mouse' in cc.sys.capabilities){
			var listener_mouse = cc.EventListener.create({
				event: cc.EventListener.MOUSE,
				swallowTouches: false,
				onMouseScroll:UpperLowerSliding.onMouseScroll
			});
			cc.eventManager.addListener(listener_mouse, this);
			
			var upArrow = new Angel(this.ver, "#sel_up_arrow.png", this.down, this);
			upArrow.setLocalZOrder(4);
			upArrow.setPosition(this.ver.width * 0.5, this.ver.height + 20);
			
			var downArrow = new Angel(this.ver, "#sel_down_arrow.png", this.up, this);
			downArrow.setLocalZOrder(4);
			downArrow.setPosition(this.ver.width * 0.5, 0 - 20);
		}
	}
});